using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace ESTPrototype
{
    public partial class TransitionPanel : UserControl
    {
        public TransitionPanel()
        {
            InitializeComponent();

            // create the list of move icons
            this.mMoveIcons = new List<PictureBox>();

            this.mRectangleOfSemiTransparentScreenThumbnail = new Rectangle(0, 0, 102, 98);
            this.mSemiTransparentImageOfScreenThumbnail = new Bitmap(102, 98);

            // a sixty percent semitransparent color matrix
            ColorMatrix colorMatrix = new ColorMatrix(new float[5][] {
                    new float[5]{1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
                    new float[5]{0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
                    new float[5]{0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
                    new float[5]{0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
                    new float[5]{0.0f, 0.0f, 0.0f, 0.0f, 1.0f} });

            // initialize the image attributes
            this.mSemiTransparentImageAttributes = new ImageAttributes();
            this.mSemiTransparentImageAttributes.SetColorMatrix(colorMatrix);

            /*using (Graphics graphics = Graphics.FromImage(this.mSemiTransparentImageOfThumbnail))
            {
                graphics.Clear(Color.LightCyan);
            }*/
        }

        public bool DrawSemiTransparentScreenThumbnail
        {
            get
            {
                return this.mDrawSemiTransparentScreenThumbnail;
            }

            set
            {
                this.mDrawSemiTransparentScreenThumbnail = value;
            }
        }

        public Bitmap SemiTransparentImageOfScreenThumbnail
        {
            get
            {
                return this.mSemiTransparentImageOfScreenThumbnail;
            }

            set
            {
                this.mSemiTransparentImageOfScreenThumbnail = value;
            }
        }

        public Point MousePositionForSemiTransparentScreenThumbnail
        {
            set
            {
                // calculate the proper position of the rectangle
                this.mRectangleOfSemiTransparentScreenThumbnail.Location = new Point(value.X - 102/2, value.Y - 98 / 2);
            }
        }

        public void AddScreenThumbnail(Thumbnail pScreenThumbnail)
        {
            // set its position
            pScreenThumbnail.Location = this.mRectangleOfSemiTransparentScreenThumbnail.Location;

            

            // create a new move icon for this thumbnail
            PictureBox moveIcon = new PictureBox();
            moveIcon.Size = new Size(30, 30);
            moveIcon.BorderStyle = BorderStyle.FixedSingle;
            moveIcon.Location = new Point(pScreenThumbnail.Left + (pScreenThumbnail.Width / 2) - (moveIcon.Width / 2), pScreenThumbnail.Top - (moveIcon.Height / 2));
            moveIcon.Tag = pScreenThumbnail;

            moveIcon.MouseDown += new MouseEventHandler(this.cMoveIconMouseDown);
            moveIcon.MouseUp += new MouseEventHandler(this.cMoveIconMouseUp);
            moveIcon.MouseMove += new MouseEventHandler(this.cMoveIconMouseMove);
            
            // finally add this move icon to oursevles
            this.Controls.Add(moveIcon);

            // then add it to our list
            this.Controls.Add(pScreenThumbnail);

            // make these controls show up in front of the existing ones
            pScreenThumbnail.BringToFront();
            moveIcon.BringToFront();
        }

        private void cMoveIconMouseDown(object pSender, MouseEventArgs pEventArgs)
        {
            this.mMovingScreenThumbnail = true;
            this.mMouseDownPositionInMoveIcon = pEventArgs.Location;
        }

        private void cMoveIconMouseUp(object pSender, MouseEventArgs pEventArgs)
        {
            this.mMovingScreenThumbnail = false;
        }

        private void cMoveIconMouseMove(object pSender, MouseEventArgs pEventArgs)
        {
            if (this.mMovingScreenThumbnail)
            {
                // save the move icon and screen thumbnail
                Control moveIcon = (Control)pSender;
                Control screenThumbnail = ((Control)moveIcon.Tag);

                // create a point for the new position
                Point newPosition = Point.Empty;

                // calculate the new position of the move icon
                newPosition.X = moveIcon.Left + pEventArgs.X - this.mMouseDownPositionInMoveIcon.X;
                newPosition.Y = moveIcon.Top + pEventArgs.Y - this.mMouseDownPositionInMoveIcon.Y;

                // calculate the halves of the screen thumbnail and the move icon
                int halfScreenThumbnailWidth = screenThumbnail.Width / 2;
                int halfMoveIconWidth = moveIcon.Width / 2;

                // make sure the actual screenthumbnail stays bounded
                if (newPosition.Y < 0)
                {
                    newPosition.Y = 0;
                }

                if (newPosition.X < halfScreenThumbnailWidth - halfMoveIconWidth)
                {
                    newPosition.X = halfScreenThumbnailWidth - halfMoveIconWidth;
                }

                // now move the move icon
                moveIcon.Location = newPosition;

                // and move its corresponding screen thumbnail
                newPosition.X += halfMoveIconWidth - halfScreenThumbnailWidth;
                newPosition.Y += moveIcon.Height / 2;
                screenThumbnail.Location = newPosition;
            }
        }

        protected override void OnPaint(PaintEventArgs pEventArgs)
        {
            if (this.mDrawSemiTransparentScreenThumbnail)
            {
                //Random r = new Random();
                //pEventArgs.Graphics.Clear(Color.FromArgb(r.Next(1, 255), r.Next(1, 255), r.Next(1, 255)));
                
                pEventArgs.Graphics.SetClip(Rectangle.Union(this.mPreviousRectangleOfSemiTransparentScreenThumbnail, this.mRectangleOfSemiTransparentScreenThumbnail));
                pEventArgs.Graphics.Clear(this.BackColor);
                //pEventArgs.Graphics.Clear(Color.HotPink);

                pEventArgs.Graphics.DrawImage(this.mSemiTransparentImageOfScreenThumbnail, this.mRectangleOfSemiTransparentScreenThumbnail, 0, 0, 102, 98, GraphicsUnit.Pixel, this.mSemiTransparentImageAttributes);

                this.mPreviousRectangleOfSemiTransparentScreenThumbnail = this.mRectangleOfSemiTransparentScreenThumbnail;
            }
        }

        private bool mDrawSemiTransparentScreenThumbnail;
        private Rectangle mRectangleOfSemiTransparentScreenThumbnail;
        private Rectangle mPreviousRectangleOfSemiTransparentScreenThumbnail;
        private Bitmap mSemiTransparentImageOfScreenThumbnail;

        private ImageAttributes mSemiTransparentImageAttributes;

        private List<PictureBox> mMoveIcons;

        private Point mMouseDownPositionInMoveIcon;
        private bool mMovingScreenThumbnail;
    }
}
